<template>
  <div class="app-container">
    <h4>课程列表</h4>
    <el-form ref="searchForm" :inline="true" :model="searchForm" label-width="120px" style="margin-left: 20px">
      <el-form-item >
        <el-input v-model="searchForm.title" placeholder="课程名称"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.status" placeholder="课程状态">
          <el-option label="未发布" value="Draft" />
          <el-option label="已发布" value="Normal" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="findPage">搜索</el-button>
        <el-button type="primary" plain @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="courseTable"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="title"
        label="课程名称"
        width="260">
      </el-table-column>
      <el-table-column
        label="课程状态"
        width="80">
        <template slot-scope="scope">
          {{scope.row.status=='Draft'?'未发布': scope.row.status=='Normal'?'已发布':'其他'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="eduCreate"
        label="添加时间"
        width="170">
      </el-table-column>
      <el-table-column
        prop="viewCount"
        label="浏览数"
        width="70">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="updateCour(scope.row.id)">编辑课程基本信息</el-button>
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="editChapter(scope.row.id)">编辑课程大纲信息</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="delCour(scope.row.id)">删除课程信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @current-change="nextPage"
      :page-size="pageCount"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {delCourse, getPage} from "@/api/course";

export default {
  name: "index",
  data(){
    return{
      searchForm:{
        pageNow:1,
        title:'',
        status:''
      },
      pageCount:4,
      total:0,
      courseTable:[]
    }
  },
  mounted() {
    this.findPage();
  },
  methods:{
    editChapter(cid){
      this.$router.push({path:"/course/chapter",query:{cId:cid}})
    },
    updateCour(cid){
      this.$router.push({path:"/course/save",query:{cId:cid}})
    },
    delCour(id){
      this.$confirm('此操作将永久删除该课程信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delCourse(id).then((res)=>{
          if(res.code==20000){
            this.$message.success("删除成功")
            this.findPage();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    resetSearch(){
      this.searchForm={
        pageNow:1,
        title:'',
        status:''
      }
    },
    nextPage(val){
      this.searchForm.pageNow=val;
      this.findPage();
    },
    findPage(){
      getPage(this.searchForm,this.pageCount).then((res)=>{
        if (res.code==20000){
          this.courseTable=res.data.records
          this.total=res.data.total
        }
      })
    }
  }
}
</script>



<style scoped lang="scss">

</style>
